package org.example.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.example.annotation.CheckPermission;
import org.example.ourEnum.AnnouncementStatus;
import org.example.pojo.domain.Announcement;
import org.example.result.Result;
import org.example.service.AnnouncementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/announcements")
@CrossOrigin
public class AnnouncementController {
    @Autowired
    private AnnouncementService announcementService;

    /** 馆长创建公告的草稿(admin) **/
    @CheckPermission({"admin"})
    @PostMapping("/createDraft")
    public Result createDraft(@RequestBody Announcement announcement) {
        announcementService.createDraft(announcement);
        return Result.success("创建公布草稿成功");
    }

    /** 通过id查看一个公告详细信息(所有角色) **/
    @CheckPermission({"visitor","admin","curator"})
    @GetMapping("/{id}")
    public Result getById(@PathVariable("id") Integer id) {
        return Result.success("通过id获得announcement成功", announcementService.getById(id));
    }

    /** 分页查看所有公告(admin) **/
    @CheckPermission({"admin"})
    @GetMapping("/list")
    public Result getAll(@RequestParam(defaultValue = "1") int page,
                         @RequestParam(defaultValue = "10") int size) {
        PageHelper.startPage(page,size);
        return Result.success("查看所有公告成功", new PageInfo<>(announcementService.getAll()));
    }

    /** 分页查看已发布的所有公告(所有角色) **/
    @CheckPermission({"visitor","admin","curator"})
    @GetMapping("/listPublished")
    public Result getPublished(@RequestParam(defaultValue = "1") int page,
                               @RequestParam(defaultValue = "10") int size) {
        PageHelper.startPage(page,size);
        return Result.success("查看所有已发布的公告成功", new PageInfo<>(announcementService.getPublished()));
    }

    /**
     * 分页查询所有草稿状态的公告(admin)
     * @param page 分页页码，默认1
     * @param size 每页条数，默认10
     */
    @CheckPermission({"admin"})
    @GetMapping("/listDrafts")
    public Result listDrafts(@RequestParam(defaultValue = "1") int page,
                             @RequestParam(defaultValue = "10") int size) {
        PageInfo<Announcement> pageInfo =
                announcementService.listByStatus("draft", page, size);
        return Result.success("分页草稿公告列表", pageInfo);
    }

    /** 更新公告(admin) **/
    @CheckPermission({"admin"})
    @PutMapping("/update")
    public Result update(@RequestBody Announcement announcement) {
        announcementService.update(announcement);
        return Result.success("更新成功");
    }
    /** 更改公告状态，将公告状态从 草稿 更改为 发布(admin) **/
    @CheckPermission({"admin"})
    @PostMapping("/changeStatus")
    public Result changeStatus(@RequestBody Announcement announcement) {
        announcementService.changeStatus(announcement.getAnnouncementId(), AnnouncementStatus.published);
        return Result.success("改变状态成功");
    }

    /** 通过id删除公告(admin) **/
    @CheckPermission({"admin"})
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") Integer id) {
        announcementService.delete(id);
        return Result.success("删除成功");
    }
}
